import SwiftUI
#if os(macOS)
import Cocoa
#else
import UIKit
#endif

public extension Color {
    init(hex: String) {
        var hexStr = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        if hexStr.hasPrefix("#") {
            hexStr = (hexStr as NSString).substring(from: 1)
        } else if hexStr.hasPrefix("0x") {
            hexStr = (hexStr as NSString).substring(from: 2)
        }
        let hexValue = UInt(hexStr, radix: 16) ?? 0
        let r = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0
        let g = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0
        let b = CGFloat(hexValue & 0x0000FF) / 255.0
        self.init(red: r, green: g, blue: b)
    }
    
    var hexString: String {
        var r :CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        var rInt, gInt, bInt: Int
        var rHex, gHex, bHex: String
        var hexColor: String
        #if os(macOS)
        NSColor(self).getRed(&r, green: &g, blue: &b, alpha: &a)
        #else
        UIColor(self).getRed(&r, green: &g, blue: &b, alpha: &a)
        #endif
        rInt = Int((r * 255))
        gInt = Int((g * 255))
        bInt = Int((b * 255))
        rHex = String(format:"%02X", rInt)
        gHex = String(format:"%02X", gInt)
        bHex = String(format:"%02X", bInt)
        hexColor = "#" + rHex + gHex + bHex
        return hexColor
    }
}

extension Color: @retroactive RawRepresentable {
    public var rawValue: String {
        return hexString
    }
    
    public init?(rawValue: String) {
        self.init(hex: rawValue)
    }
}

public extension Color {
    struct Light {
        public static let accent = Color(hex: "#fc3c4e")
        public static let background = Color(hex: "#f7f9fc")
        public static let normal = Color(hex: "#000000")
        public static let week = Color(hex: "#000000")
        public static let festival = Color(hex: "#fc3c4e")
        public static let current = Color(hex: "#fc3c4e")
        public static let calendar = Color(hex: "#0eb83a")
        public static let reminder = Color(hex: "#0eb83a")
        public static let work = Color(hex: "#f0c239")
        public static let rest = Color(hex: "#549688")
        public static let taboo = Color(hex: "#FF6666")
        public static let avoid = Color(hex: "#cccccc")
    }

    struct Dark {
        public static let accent = Color(hex: "#fc3c4e")
        public static let background = Color(hex: "#13131A")
        public static let normal = Color(hex: "#ffffff")
        public static let week = Color(hex: "#ffffff")
        public static let festival = Color(hex: "#fc3c4e")
        public static let current = Color(hex: "#fc3c4e")
        public static let calendar = Color(hex: "#0eb83a")
        public static let reminder = Color(hex: "#0eb83a")
        public static let work = Color(hex: "#f0c239")
        public static let rest = Color(hex: "#549688")
        public static let taboo = Color(hex: "#FF6666")
        public static let avoid = Color(hex: "#cccccc")
    }
}

public extension Color {
    var contrastColor: Color {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
        #if os(macOS)
        let color = NSColor(self).usingColorSpace(.sRGB)
        color?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        #else
        let color = UIColor(self)
        color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        #endif
        let luminance = 0.299 * red + 0.587 * green + 0.114 * blue
        return luminance > 0.5 ? .black : .white
    }
}
